package cn.com.main;

import java.io.IOException;
import java.util.Base64;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpRequestExample {

    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();

        String url = "http://localhost:8081/service/rest/v1/repositories";
        String username = "admin";
        String password = "1q2w3e$R";

        // 构建请求
        Request request = new Request.Builder()
                .url(url)
                .header("Authorization", "Basic " + base64Encode(username + ":" + password))
                .build();

        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            // 打印响应内容
            System.out.println(response.body().string());
        }
    }

    // Base64编码工具方法
    private static String base64Encode(String data) {
        return Base64.getEncoder().encodeToString(data.getBytes());
    }
}
